package com.qxzn.app.util;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static final String yearToSecPattern = "yyyy-MM-dd HH:mm:ss";
    public static final String yearToDatePattern = "yyyy-MM-dd";
    public static final String yearToDatePattern1 = "yyyy-MM-dd ";
    public static final String hourToSecPattern = "HH:mm:ss";
    public static final String hourToMinPattern = "HH:mm";
    public static final String yearToSecPattern1 = "yyyy年MM月dd日  HH:mm";
    public static final String yearToSecPattern2 = "yyyy-MM-dd_HH-mm-ss";
    public static final String yearToMSecPattern1 = "yyyy-MM-dd'T'HH:mm:ss.SSS Z";
    public static final String yearToDatePattern2 = "yyyy.MM.dd";
    public static final String monthToDatePattern1 = "MM月dd日";
    public static final String hourToMinPatterns = "hh:mm";

    public static String formMillisToStr(long millis) {
        return formMillisToStr(millis, null);
    }

    public static String formMillisToStr(long millis, String pattern) {
        SimpleDateFormat format;
        if (pattern == null || pattern.length() == 0) {
            format = new SimpleDateFormat(yearToSecPattern);
        } else {
            format = new SimpleDateFormat(pattern);
        }
        return format.format(millis);
    }

    public static String formDateToStr(Date date) {
        return formDateToStr(date, null);
    }

    public static String formDateToStr(Date date, String pattern) {
        SimpleDateFormat format;
        if (pattern == null || pattern.length() == 0) {
            format = new SimpleDateFormat(yearToSecPattern);
        } else {
            format = new SimpleDateFormat(pattern);
        }
        return format.format(date);
    }

    public static Long parseStrToMillis(String timeStr, String pattern) {
        SimpleDateFormat format;
        try {
            if (pattern == null || pattern.length() == 0) {
                format = new SimpleDateFormat(yearToSecPattern);
            } else {
                format = new SimpleDateFormat(pattern);
            }
            return format.parse(timeStr).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 获得本周几0点时间戳
    public static long getMondayMillis(int weekDate) {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
//        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, weekDate);
        return cal.getTimeInMillis();
    }

    //中国星期下标(星期一开始)转标准星期下标(星期天开始)
    public static int formCNWeekDateToStd(int weekDate) {
        if (weekDate == 7) {
            return 1;
        } else {
            return weekDate + 1;
        }
    }

    public static String getWeekStrByWeekNum(String week, String extStr) {
        if (!TextUtils.isEmpty(week)) {
            String[] weeks = week.split(",");
            if (weeks != null) {
                week = "";
                for (int i = 0; i < weeks.length; i++) {
                    if (i != 0) {
                        week += "、";
                    }
                    switch (weeks[i]) {
                        case "1":
                            week += extStr + "一";
                            break;
                        case "2":
                            week += extStr + "二";
                            break;
                        case "3":
                            week += extStr + "三";
                            break;
                        case "4":
                            week += extStr + "四";
                            break;
                        case "5":
                            week += extStr + "五";
                            break;
                        case "6":
                            week += extStr + "六";
                            break;
                        default:
                            week += extStr + "日";
                            break;
                    }
                }
            }
        }
        return week;
    }

    public static String formatMillisToVoiceLen(long millis) {
        if (millis < 60000) {
            return (DateUtil.formMillisToStr(millis, "ss::") + "").replace(":", "'");
        } else {
            return (DateUtil.formMillisToStr(millis, "mm:ss::") + "").replace(":", "'");
        }
    }

}
